//定义js命名空间
Ext.namespace("FsCarInfo.FsBcar");

//FsBcar FieldArray
FsCarInfo.FsBcar.BaseFieldsArray = new Array(
	{ name: 'Bcarid', type: 'string' },
	{ name: 'IdCard', type: 'FsBcar' },
	{ name: 'Type', type: 'string' },
	{ name: 'CarNo', type: 'string' },
	{ name: 'Owner', type: 'string' },
	//{ name: 'Dept', type: 'InfraDepartment' },
	{ name: 'Dept' },
	{ name: 'Description', type: 'string' },
	{ name: 'CreateTime', type: 'DateTime' },
	{ name: 'IsDelete', type: 'bool' },
	{ name: 'Version', type: 'int' },
	{ name: 'State', type: 'int' },
);

//FsBcar JsonReader
FsCarInfo.FsBcar.JsonReader = Ext.extend(Infrastructure.BaseJsonReader, {
    constructor: function (cfg) {
        cfg = cfg || {};
        FsCarInfo.FsBcar.JsonReader.superclass.constructor.call(this, Ext.apply({
            fields: FsCarInfo.FsBcar.BaseFieldsArray
        }, cfg));
    }
});

//FsBcar JsonStore
FsCarInfo.FsBcar.JsonStore = Ext.extend(Infrastructure.BaseJsonStore, {
    constructor: function (cfg) {
        cfg = cfg || {};
        FsCarInfo.FsBcar.JsonStore.superclass.constructor.call(this, Ext.apply({
            url: "/FsBcar/GetPlist",
            remoteSort: true,
            reader: new FsCarInfo.FsBcar.JsonReader(),
            listeners: {
                'remove': function (thiz, record, index) {
                    Ext.Ajax.request({
                        url: '/FsBcar/Delete?Id=' + record.data.Id,
                        success: function (response, opts) {
                            var result = Ext.util.JSON.decode(response.responseText);
                            if (result.success == false) {
                                JsHelper.ShowError(result.msg);
                                this.insert(0, record);
                            }
                        }
                    });
                }
            }
        }, cfg));
    }
});

//FsBcar Default GridToolbar
FsCarInfo.FsBcar.DefaultToolbar = Ext.extend(Ext.Toolbar, {
    tbargrid: null,
    constructor: function (cfg) {
        cfg = cfg || {};
        FsCarInfo.FsBcar.DefaultToolbar.superclass.constructor.call(this, Ext.apply({
            items: [{
                xtype: "tbaraddbtn",
                handler: function (btn, e) {
                    var FsBcarEditForm = new FsCarInfo.FsBcar.EditForm({
                        buttons: [new Ext.ux.SubmitBtn({
                            handler: function () {
                                if (FsBcarEditForm.getForm().isValid()) {
                                    FsBcarEditForm.getForm().submit({
                                        url: '/FsBcar/SaveOrUpdate',
                                        params: {
                                            'Id': (updateEdit) ? id : ""
                                        },
                                        success: function (form, action) {
											//刷新表格
                                            cfg.tbargrid.getStore().setDefaultSort('CreateTime', 'DESC');
											cfg.tbargrid.load({
                                                params: {
                                                    start: 0,
                                                    limit: ALL_PAGESIZE_SETTING
                                                }
                                            });
											// 关闭窗口
                                            FsBcarEditWin.destroy();
                                        },
                                        failure: function (form, action) {
                                            JsHelper.ShowError(action.result.msg);
                                        }
                                    });
                                }
                            }
                        }),
                        new Ext.ux.CancelBtn({
                            handler: function () {
                                FsBcarEditWin.destroy();
                            }
                        })]
                    });
                    var FsBcarEditWin = new FsCarInfo.FsBcar.EditWin({
                        items: [FsBcarEditForm]
                    });
                    FsBcarEditWin.show();
                }
            }, "-", {
                xtype: "tbardelbtn",
                handler: function (btn, e) {
                    JsHelper.DelConfirm(function (btn) {
                        if (btn == 'yes') {
                            alert(btn);
                            var s = cfg.tbargrid.getSelectionModel().getSelections();
                            for (var i = 0, r; r = s[i]; i++) {
                                cfg.tbargrid.getStore().remove(r);
                            }
                        }
                    });
                }
            }]
        }, cfg));
    }
});

//FsBcar Default GridColumnMode
FsCarInfo.FsBcar.DefaultColumnModel = Ext.extend(Ext.grid.ColumnModel, {
    sm: null,
    constructor: function (cfg) {
        cfg = cfg || {};
        FsCarInfo.FsBcar.DefaultColumnModel.superclass.constructor.call(this, Ext.apply({
            columns: [
                new Ext.grid.RowNumberer(),
                new Ext.grid.CheckboxSelectionModel(), 
				{ header: "ID卡号", dataIndex: 'IdCard' }, 
				{ header: "类型", dataIndex: 'Type' }, 
				{ header: "车牌", dataIndex: 'CarNo' }, 
				{ header: "所有人", dataIndex: 'Owner' }, 
				{ header: "所属机构", dataIndex: 'Dept.Name' }, 
				{ header: "机构名", dataIndex: 'Department' }, 
				{ header: "描述信息", dataIndex: 'Description' }, 
				{ header: "状态", dataIndex: 'State' }
            ]
        }, cfg));
    }
});

//FsBcar DataGrid
FsCarInfo.FsBcar.GridPanel = Ext.extend(Ext.grid.GridPanel, {
    constructor: function (cfg) {
        cfg = cfg || {};
        FsCarInfo.FsBcar.GridPanel.superclass.constructor.call(this, Ext.apply({
            stripeRows: true,
            frame: true,
            border: true,
            layout: "fit",
            loadMask: true,
            enableColumnMove: false,
            store: new FsCarInfo.FsBcar.JsonStore(),
            sm: new Ext.grid.CheckboxSelectionModel(),
            cm: new FsCarInfo.FsBcar.DefaultColumnModel(),
            tbar: new FsCarInfo.FsBcar.DefaultToolbar({
                tbargrid: this
            }),
            bbar: new Infrastructure.BasePagingToolbar({
                store: cfg.store,
                plugins: cfg.plugins
            })
        }, cfg));
    }
});

//FsBcar EditForm
FsCarInfo.FsBcar.EditForm = Ext.extend(Infrastructure.BaseForm, {
    constructor: function (cfg) {
        cfg = cfg || {};

        //Form第一列
        // 定义表单列
		var fieldColumn = [{
			fieldLabel: "ID卡号",
			width: 150,
			maxLength: 36,
			xtype: 'textfield',
			name: 'IdCard'
		}, {
			fieldLabel: "类型",
			width: 150,
			maxLength: 10,
			xtype: 'textfield',
			name: 'Type'
		}, {
			fieldLabel: "车牌",
			width: 150,
			maxLength: 10,
			xtype: 'textfield',
			name: 'CarNo'
		}, {
			fieldLabel: "所有人",
			width: 150,
			maxLength: 30,
			xtype: 'textfield',
			name: 'Owner'
		}, {
			fieldLabel: "所属机构",
			width: 150,
			maxLength: 36,
			xtype: 'textfield',
			name: 'Deptid'
		}, {
			fieldLabel: "机构名",
			width: 150,
			maxLength: 50,
			xtype: 'textfield',
			name: 'Department'
		}, {
			fieldLabel: "描述信息",
			width: 150,
			maxLength: 50,
			xtype: 'textfield',
			name: 'Description'
		}, {
			fieldLabel: "状态",
			width: 150,
			maxLength: 0,
			xtype: 'textfield',
			name: 'State'
		}, ]

        //Form第二列
        var secondColumn = [{
            fieldLabel: 'Column2',
            width: 150
        }];

        //Form单列
        var singleColumn = [{
            xtype: 'textarea',
            fieldLabel: '描述',
            //name: 'Remark',
            height: 50,
            width: 430
        }];

        FsCarInfo.FsBcar.EditForm.superclass.constructor.call(this, Ext.apply({
            items: [{
                layout: 'column',
                // 注意 现在使用的是两列 要是三列或更多
                // 请改变下面的items
                // 并在上面定义每个列的组件集合
                items: [{
                    columnWidth: .5,
                    bodyStyle: 'padding:1px',
                    layout: 'form',
                    defaultType: 'textfield',
                    items: firstColumn
                }, {
                    columnWidth: .5,
                    bodyStyle: 'padding:1px',
                    layout: 'form',
                    defaultType: 'textfield',
                    items: secondColumn
                }]
            }, singleColumn],
            reader: new FsCarInfo.FsBcar.JsonReader()
        }, cfg));
    }
});

//FsBcar DetailWin
FsCarInfo.FsBcar.EditWin = Ext.extend(Ext.Window, {
    constructor: function (cfg) {
        cfg = cfg || {};
        FsCarInfo.FsBcar.EditWin.superclass.constructor.call(this, Ext.apply({
            title: 'FsBcar Infomation',
            iconCls: 'application_form',
            width: 600,
            resizable: false,
            constrain: true,
            autoHeight: true,
            modal: true,
            closeAction: 'close',
            plain: true,
            items: [new FsCarInfo.FsBcar.EditForm()],
            buttonAlign: 'center'
        }, cfg));
    }
});

	